<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */

defined('IN_ADMIN_COMPONENT_CONTROLLER') or die();

require_once('includes/functions/configuration_functions.php');
class ConfigurationComponent
{
	function ConfigurationComponent()
	{
	}

	function action(&$params, $action='query')
	{
		if ($action == 'Update') 
			return $this->edit($params);
		else 
			return $this->query($params);
	}

	function query(&$params)
	{		
		$handle = get_class($this);
		$groupId = (int)$params['configuration_group_id'];

		$sqlQuery = 'select configuration_id, configuration_title, configuration_key, configuration_value, configuration_description, ' . 
		'configuration_format, configuration_values, ' .
		"DATE_FORMAT(date_added, '%Y-%m-%d %H:%i:%s') as date_added, " .
		"DATE_FORMAT(last_modified, '%Y-%m-%d %H:%i:%s') as last_modified " .
		'from ' . TABLE_CONFIGURATION . ' ' .
		'where configuration_group_id = ' . $groupId . ' ' .
		'order by sort_order';
		$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
		$i = 0;
		while($row = mysql_fetch_array($dataReturned))
		{
			$value{$handle}{$i}{'group_id'}= $groupId;
			$value{$handle}{$i}{'id'}=(int)$row['configuration_id'];
			$value{$handle}{$i}{'title'}= $row['configuration_title'];
			$value{$handle}{$i}{'key'}= $row['configuration_key'];
			$value{$handle}{$i}{'description'}= $row['configuration_description'];
			$value{$handle}{$i}{'value'}= $row['configuration_value'];
			$value{$handle}{$i}{'format'}= $row['configuration_format'];
			$configuration_values = $row['configuration_values'];
			$value{$handle}{$i}{'date_added'}= $row['date_added'];
			$value{$handle}{$i}{'last_modified'}= $row['last_modified'];
			
			$values = array();
			if ($configuration_values != null && $configuration_values != 'NULL' && strlen(trim($configuration_values)) > 0) {
				$pos = strpos($configuration_values, ',');
				if ($pos === false) {
					eval('$values = ' . $configuration_values . ';');
				}
				else {
					$values_untrimmed = explode(',', $configuration_values);
					reset($values_untrimmed);
					$valueIndex = 0;
					while (list(, $value_untrimmed) = each($values_untrimmed)) 
					{
						$value_trimmed = trim($value_untrimmed);
						$values[$valueIndex]['value'] = $value_trimmed;
						$values[$valueIndex]['title'] = $value_trimmed;
						$valueIndex++;
					}
				}
				$value{$handle}{$i}{'values'}= $values;
			}

			$i++;
		}

		return $value;
	}

	function edit(&$params)
	{
		$groupId = (int)$params['configuration_group_id'];
		$sqlQuery = 'select configuration_key ' . 
		'from ' . TABLE_CONFIGURATION . ' ' .
		'where configuration_group_id = ' . $groupId . ' ' .
		'order by sort_order';
		$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
		$i = 0;
		while($row = mysql_fetch_array($dataReturned))
		{
			$key = $row['configuration_key'];

			if (isset($params[$key])) {
				$newValue = $params[$key];
				if ($key == 'MASTER_PASS') $newValue = MD5($newValue);
				$sql = 'update ' . TABLE_CONFIGURATION . ' ' .
				'set configuration_value = \'' . mysql_real_escape_string($newValue) . '\', ' .
				'last_modified=now() ' .
				'where configuration_key = \'' . $key . '\'';
				mysql_query($sql) or die(mysql_error());
			}
		}

		clean_cache();
		return $this->query($params);

	}		
}
?>
